package com.metaboot.demo.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.metaboot.common.core.page.PageUtils;
import com.metaboot.common.core.page.TableDataInfoPlus;
import com.metaboot.common.utils.StringUtils;
import com.metaboot.demo.domain.DemoSchool;
import com.metaboot.demo.domain.DemoStudent;
import com.metaboot.demo.mapper.DemoSchoolMapper;
import com.metaboot.demo.mapper.DemoStudentMapper;
import com.metaboot.demo.service.IDemoSchoolService;

/**
 * 主子表演示Service业务层处理
 * 
 * @author theodo
 * @date 2021-12-06
 */
@Service
public class DemoSchoolServiceImpl extends ServiceImpl<DemoSchoolMapper, DemoSchool> implements IDemoSchoolService {
	@Autowired
	private DemoSchoolMapper demoSchoolMapper;
	
	@Autowired
	private DemoStudentMapper demoStudentMapper;

	/**
	 * 查询主子表演示
	 * 
	 * @param id 主子表演示主键
	 * @return 主子表演示
	 */
	@Override
	public DemoSchool selectDemoSchoolById(Long id) {
		return demoSchoolMapper.selectDemoSchoolById(id);
	}
	
	/**
	 * 获取查询条件
	 * 
	 * @param demoSchool 单表演示
	 * @return 单表演示
	 */
	@SuppressWarnings("unused")
	private LambdaQueryWrapper<DemoSchool> buildQueryWrapper(DemoSchool demoSchool) {
        Map<String, Object> params = demoSchool.getParams();
        LambdaQueryWrapper<DemoSchool> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(demoSchool.getName()), DemoSchool::getName, demoSchool.getName());
        return lqw;
    }

	/**
	 * 查询主子表演示列表
	 * 
	 * @param demoSchool 主子表演示
	 * @return 主子表演示
	 */
	@Override
	public List<DemoSchool> selectDemoSchoolList(DemoSchool demoSchool) {
		return demoSchoolMapper.selectList(buildQueryWrapper(demoSchool));
	}
	
	/**
     * 查询主子表演示列表-分页查询
     * 
     * @param demoSchool 主子表演示
     * @return 主子表演示集合
     */
    public TableDataInfoPlus<DemoSchool> selectPageDemoSchoolList(DemoSchool demoSchool) {
    	return PageUtils.buildDataInfo(page(PageUtils.buildPage(), buildQueryWrapper(demoSchool)));
    }

	/**
	 * 新增主子表演示
	 * 
	 * @param demoSchool 主子表演示
	 * @return 结果
	 */
	@Transactional
	@Override
	public int insertDemoSchool(DemoSchool demoSchool) {
		int rows = demoSchoolMapper.insert(demoSchool);
		insertDemoStudent(demoSchool);
		return rows;
	}

	/**
	 * 修改主子表演示
	 * 
	 * @param demoSchool 主子表演示
	 * @return 结果
	 */
	@Transactional
	@Override
	public int updateDemoSchool(DemoSchool demoSchool) {
		demoSchoolMapper.deleteDemoStudentBySchoolId(demoSchool.getId());
		insertDemoStudent(demoSchool);
		return demoSchoolMapper.updateById(demoSchool);
	}

	/**
	 * 批量删除主子表演示
	 * 
	 * @param ids 需要删除的主子表演示主键
	 * @return 结果
	 */
	@Transactional
	@Override
	public int deleteDemoSchoolByIds(Collection<Long> ids) {
		demoSchoolMapper.deleteDemoStudentBySchoolIds(ids);
		return demoSchoolMapper.deleteDemoSchoolByIds(ids);
	}

	/**
	 * 删除主子表演示信息
	 * 
	 * @param id 主子表演示主键
	 * @return 结果
	 */
	@Transactional
	@Override
	public int deleteDemoSchoolById(Long id) {
		demoSchoolMapper.deleteDemoStudentBySchoolId(id);
		return demoSchoolMapper.deleteDemoSchoolById(id);
	}

	/**
	 * 新增学生管理信息
	 * 
	 * @param demoSchool 主子表演示对象
	 */
	public void insertDemoStudent(DemoSchool demoSchool) {
		List<DemoStudent> demoStudentList = demoSchool.getDemoStudentList();
		Long id = demoSchool.getId();
		if (StringUtils.isNotNull(demoStudentList)) {
			for (DemoStudent demoStudent : demoStudentList) {
				demoStudent.setSchoolId(id);
				demoStudentMapper.insert(demoStudent);
			}
		}
	}
}
